package org.softmed.rest.generation.builders;

import java.util.List;

import org.softmed.rest.config.Handler;
import org.softmed.rest.config.HttpMethod;
import org.softmed.rest.config.MimeTypeHandler;
import org.softmed.rest.config.Module;
import org.softmed.rest.config.Parameter;
import org.softmed.rest.config.Resource;
import org.softmed.rest.generation.HandlerSuite;

public class BaseCreator {

	// String groovyScriptDir = "services.administration";
	// String genericGetXMLScript = ".generic_get_xml";

	public Resource createBasicPostXML(Class type, Module module,
			String targetURI, String providerName, String xmlSuite,
			HandlerSuite handlerSuite) {
		return createPostXML(module, type, targetURI, providerName, xmlSuite,
				handlerSuite);
	}

	public Resource createPostXML(Module module, Class type, String targetURI,
			String providerName, String xmlSuite, HandlerSuite handlerSuite) {
		Resource resource;
		resource = new Resource();
		resource.setActive(true);
		resource.setTargetUri(targetURI);

		module.getResources().add(resource);

		addMethodMediaType(resource, type, "application/xml", "POST",
				providerName, xmlSuite, handlerSuite);

		return resource;
	}

	public HttpMethod addMethodMediaType(Resource resource, Class type,
			String mimetype, String httpMethod, String providerName,
			String xmlSuite, HandlerSuite handlerSuite) {
		HttpMethod method;
		MimeTypeHandler mime;
		Handler handler;

		method = new HttpMethod();
		method.setName(httpMethod);
		method.setActive(true);

		resource.getHttpMethods().add(method);

		mime = createMimeTypeHandler(method, mimetype, providerName, xmlSuite,
				handlerSuite);

		List<Parameter> params = mime.getHandler().getParameters();
		params.add(new Parameter("class", type.getCanonicalName()));
		params.add(new Parameter("xml-suite", xmlSuite));

		return method;
	}

	public MimeTypeHandler createMimeTypeHandler(HttpMethod method,
			String mimetype, String providerName, String xmlSuite,
			HandlerSuite handlerSuite) {
		MimeTypeHandler mime;
		Handler handler;
		mime = new MimeTypeHandler();
		mime.setMimeType(mimetype);

		method.getMimeTypeHandlers().add(mime);

		handler = new Handler();
		handler.setActive(true);
		handler.setPath(handlerSuite.getGenericCreateNewObjectXML());

		handler.getParameters().add(
				new Parameter("provider-name", providerName));
		handler.getParameters().add(new Parameter("xml-suite", xmlSuite));

		mime.setHandler(handler);

		return mime;
	}

	public HttpMethod getPagedList(Resource resource, Class type,
			String providerName, String xmlSuite, HandlerSuite handlerSuite) {
		HttpMethod method;
		MimeTypeHandler mime;
		Handler handler;

		// get
		method = new HttpMethod();
		method.setName("GET");
		method.setActive(true);

		resource.getHttpMethods().add(method);

		mime = new MimeTypeHandler();
		mime.setMimeType("application/xml");
		method.getMimeTypeHandlers().add(mime);

		handler = new Handler();
		handler.setActive(true);
		handler.setPath(handlerSuite.getGetPagedListXML());
		handler.getParameters().add(
				new Parameter("class", type.getCanonicalName()));
		handler.getParameters().add(
				new Parameter("provider-name", providerName));
		handler.getParameters().add(new Parameter("xml-suite", xmlSuite));

		mime.setHandler(handler);

		return method;
	}

	public Resource createGetResourceAsXML(Module module, String targetURI,
			String parameter, Class type, String providerName, String xmlSuite,
			HandlerSuite handlerSuite) {
		Resource resource;
		resource = new Resource();
		resource.setActive(true);
		resource.setTargetUri(targetURI);
		module.getResources().add(resource);

		createGenericGetXMLMethod(resource, parameter, type, providerName,
				xmlSuite, handlerSuite);
		return resource;
	}

	public void createGenericGetXMLMethod(Resource resource, String parameter,
			Class type, String providerName, String xmlSuite,
			HandlerSuite handlerSuite) {
		HttpMethod method;
		MimeTypeHandler mime;
		Handler handler;

		// get
		method = new HttpMethod();
		method.setName("GET");
		method.setActive(true);

		resource.getHttpMethods().add(method);

		mime = new MimeTypeHandler();
		mime.setMimeType("application/xml");

		method.getMimeTypeHandlers().add(mime);

		handler = new Handler();
		handler.setActive(true);
		// handler.setPath(groovyScriptDir + genericGetXMLScript);
		// handler.setPath(GenericGetXML.class.getCanonicalName());
		handler.setPath(handlerSuite.getGenericGetXml());

		mime.setHandler(handler);

		handler.getParameters().add(
				new Parameter("class", type.getCanonicalName()));
		handler.getParameters().add(new Parameter("parameter", parameter));
		handler.getParameters().add(
				new Parameter("provider-name", providerName));
		handler.getParameters().add(new Parameter("xml-suite", xmlSuite));
	}

	public void createDeleteResource(Resource resource, String string,
			String parameter, Class type, String providerName, String xmlSuite,
			HandlerSuite handlerSuite) {
		HttpMethod method;
		MimeTypeHandler mime;
		Handler handler;

		// get
		method = new HttpMethod();
		method.setName("DELETE");
		method.setActive(true);

		resource.getHttpMethods().add(method);

		handler = new Handler();
		handler.setActive(true);
		handler.setPath(handlerSuite.getGenericDeleteObject());

		method.setHandler(handler);

		handler.getParameters().add(
				new Parameter("class", type.getCanonicalName()));
		handler.getParameters().add(new Parameter("parameter", parameter));
		handler.getParameters().add(
				new Parameter("provider-name", providerName));
		handler.getParameters().add(new Parameter("xml-suite", xmlSuite));

	}

	public void createPutResource(Resource resource, String string,
			String parameter, Class type, String providerName, String xmlSuite,
			HandlerSuite handlerSuite) {
		HttpMethod method;
		MimeTypeHandler mime;
		Handler handler;

		// get
		method = new HttpMethod();
		method.setName("PUT");
		method.setActive(true);

		resource.getHttpMethods().add(method);

		mime = new MimeTypeHandler();
		mime.setMimeType("application/xml");

		method.getMimeTypeHandlers().add(mime);

		handler = new Handler();
		handler.setActive(true);
		handler.setPath(handlerSuite.getGenericPutObject());

		mime.setHandler(handler);

		handler.getParameters().add(
				new Parameter("class", type.getCanonicalName()));
		handler.getParameters().add(new Parameter("parameter", parameter));
		handler.getParameters().add(
				new Parameter("provider-name", providerName));
		handler.getParameters().add(new Parameter("xml-suite", xmlSuite));

	}
}
